/*
 * @Author: Setsuna
 * @Email: 601890383@qq.com
 * @FilePath: \src\pages\QuotaManage\QuotaAdjust\component\LinkDetail.tsx
 * @Date: 2021-09-06 09:10:22
 * @LastEditTime: 2022-02-23 10:57:58
 */
import React from 'react'
import moment from 'moment'
import { http } from '@/lib'
import { Table } from 'antd'
import quotaApis from '@/apis/AboutQuota'
import './index.scss'

interface IProps {
  record: any
  type: number
}
interface IState {
  values?: any
  dataSource?: any
  page?: number
  total?: number
  size?: number
  loading?: boolean
}
export default class LinkDetail extends React.Component<IProps, IState> {
  private record = this.props.record
  private STAGE_STATUS = {
    EXECUTED: '已执行',
    EXECUTION: '执行中',
    UNEXECUTED: '待执行',
  }
  constructor(props: IProps, state: IState) {
    super(props)
    this.state = {
      page: 1,
      size: 10,
      total: 0,
      dataSource: [],
      loading: false,
    }
  }
  componentDidMount() {
    this.getOrderTableData()
  }
  render() {
    return (
      <>
        <table className="detail-table">
          <tbody>
            <tr>
              <td>考核年度：{this.record.stageYear}</td>
              <td>品牌公司：{this.record.brandName}</td>
              <td>配额品项：{this.record.prodBarname}</td>
            </tr>
            <tr>
              <td>考核阶段：{this.record.stageName}</td>
              <td>配额有效期：{`${this.record.stageBeginTime}-${this.record.stageEndTime}`}</td>
              <td>状态：{this.STAGE_STATUS[this.record.stageStatus]}</td>
            </tr>
          </tbody>
        </table>

        {
          <Table
            rowKey={(record, index) => index + ''}
            columns={[
              { title: '序号', dataIndex: 'id', render: (text, record, index) => index + 1 },
              { title: '物料分类编号', dataIndex: 'prodBarcode' },
              { title: '物料号描述', dataIndex: 'matName' },
              { title: '订单编号', dataIndex: 'orderNum' },
              { title: '订单数量', dataIndex: 'skuNum' },
              {
                title: '下单时间',
                dataIndex: 'taskStartTime',
                render: (text, record: any, index) => {
                  return record.taskStartTime ? moment(+record.taskStartTime).format('YYYY-MM-DD HH:mm:ss') : ''
                },
              },
            ]}
            dataSource={this.state.dataSource}
            onChange={this.handleChangePage}
            loading={this.state.loading}
            pagination={{
              current: this.state.page,
              pageSize: this.state.size,
              total: this.state.total,
              showQuickJumper: true,
              showSizeChanger: true,
              pageSizeOptions: ['10', '20', '30', '40'],
              showTotal: (total, range) => `共${total}项，当前${range[0]}-${range[1]}项`,
            }}
          />
        }
        <div>&nbsp;</div>
      </>
    )
  }

  private handleChangePage = pagination => {
    const { current: page, pageSize: size } = pagination
    this.setState({ page, size }, () => {
      this.getOrderTableData()
    })
  }

  private getOrderTableData = async () => {
    let { businessItemNumber, businessItemNumberWay } = this.record
    let { type } = this.props
    let number = type === 0 ? businessItemNumber : businessItemNumberWay
    if (!number) {
      return false
    }
    this.setState({ loading: true })
    try {
      let list: any = await http.post(quotaApis.getQuotaOrderPageNew, {
        page: this.state.page,
        size: this.state.size,
        conditions: {
          number,
        },
      })
      const dataSource = list.data?.content

      this.setState({
        dataSource,
        page: list.data.number,
        size: list.data.size,
        total: list.data.totalElements,
        loading: false,
      })
    } catch (error) {
      this.setState({
        loading: false,
      })
    }
  }
}
